package simple.zombie;

import com.golden.gamedev.object.*;
import com.golden.gamedev.object.background.*;

// JFC
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;

import java.awt.event.KeyEvent;

public class MainEntry extends Game {
	
	public static gameStatus = "";
	GameFont font; // bitmap game font
	MainMenu menu;
  Sprite zombieSprite;
  Zombie zombie;
  Background  background;
	
	public void initResources() {
        // initialization of game variables
		font = fontManager.getFont(getImages( "images/simple-font.png", 20, 3 ), 
            " !            .,0123456789:   -? ABCDEFGHIJKLMNOPQRSTUVWXYZ " );

		//menu = new MainMenu();
    zombieSprite = new AnimatedSprite( getImages( "images/zombie_topdown.png", 36, 8 ), 200, 200 );
    zombieSprite.setAnimate(true);
    zombieSprite.setLoopAnim(true);
    //zombie = new Zombie( 200, 200, 5, zombieSprite );
    
    background = new ColorBackground(Color.YELLOW, 800, 600);
    zombieSprite.setBackground( background );
  }

  public void update(long elapsedTime) {
		checkStatus();
    
    zombieSprite.update(elapsedTime);
		
    if( bsInput.getKeyPressed() != bsInput.NO_KEY ) {
      updateMenu();
    }
    
		if (click()) {
      System.out.println("Mouse LEFT BUTTON is pressed with X: ${getMouseX()}, Y: ${getMouseY()}");
      if( menu && menu.isShowMenu() ) {
        menu.pointerClick( getMouseX(), getMouseY() );
      }
    }
    if (bsInput.isMouseDown(MouseEvent.BUTTON3)) {
      System.out.println("Mouse RIGHT BUTTON is being pressed");
    }
  }

  public void render(Graphics2D g) {
    background.render(g);
    zombieSprite.render(g);
    
		g.setColor(Color.GRAY);
    g.fillRect(0, 0, getWidth(), getHeight());
    
		if( menu && menu.isShowMenu() ) {
			menu.getMenuContent().eachWithIndex() { menuItem, index ->
				font.drawString( g, menuItem, 200, index*25 + 200 );
				if( menu.getSelectedMenuItem() == menuItem ) {
					g.setColor( Color.RED ) ;
//					g.drawRect( 198, index*25 + 198, 130, 20 );
					g.drawRect( menu.selectorInfo.X, menu.selectorInfo.Y, menu.selectorInfo.W, menu.selectorInfo.H );
				}
			}
		}
		if( menu && menu.isShowCredit() ) {
			menu.getCreditContent().eachWithIndex() { menuItem, index ->
				font.drawString( g, menuItem, 200, index*25 + 200 );
			}		
		}
  }
	
	public static void checkStatus() {
		if( gameStatus == "exit" ) {
			exitGame();
		}
	}
  
  public void updateMenu() {
    // def key = bsInput.getKeyPressed();
    // println "Key pressed code: ${key}";
    
    if( menu ) {
      if( keyPressed( KeyEvent.VK_ESCAPE ) ) {
        menu.switchMenuStatus();
      }
      if( menu.isShowMenu() ) {
        if( keyPressed( KeyEvent.VK_UP ) ) {
          menu.menuUp();
        }
        if( keyPressed( KeyEvent.VK_DOWN ) ) {
          menu.menuDown();
        }
        if( keyPressed( KeyEvent.VK_ENTER ) ) {
          menu.callAction();
        }
      }
    }
  }
	
	public static void exitGame() {
		System.exit(0);
	}

	public MainEntry() {
	
	}
	
	public static void main( String[] args ) {
		GameLoader game = new GameLoader();
		game.setup(new MainEntry(), new Dimension(640,480), false);
		game.start();
		gameStatus = "start";
	}
}